package com.daykw.ip.task;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.quartz.DisallowConcurrentExecution;
import org.springframework.stereotype.Service;

import com.daykw.ip.http.HttpBase;
import com.daykw.ip.model.Ips;
import com.daykw.ip.util.Const;
import com.daykw.ip.util.SpringContextHolder;

//@DisallowConcurrentExecution
//@Service
public class HJIpProxyTask extends CustmTask {
	private static Logger logger = Logger.getLogger(HJIpProxyTask.class);

	@Override
	public void doStart() {

		try {
			/**
			 * 5分钟切换一次ip
			 */

			if (QuartzManager.getTrigger("HJIpProxyTask") == null) {
				QuartzManager.addJob("HJIpProxyTask", this, "0/30 * * * * ?");
			} else {
				logger.error("############已经存在任务：HJIpProxyTask############");
			}
		} catch (Exception e) {
			logger.error("", e);
		}

	}//

	@Override
	public void doWork() {
		try {
			if (SpringContextHolder.getApplicationContext() == null) {
				try {
					TimeUnit.SECONDS.sleep(3);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				logger.info("等待上下文启动");
			}
			//NutDao xhsDao = SpringContextHolder.getBean("xhsDao");
			NutDao bdsDao = SpringContextHolder.getBean("bdsDao");
			DateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String nowtime = FORMAT.format(new Date());

			try {
				String url = Const.IP_HUOJIAN;
				String iplist = new HttpBase().get(url, "utf-8", null);

				String[] proxys = iplist.split("\r\n");
				if (proxys.length == 1) {
					proxys = iplist.split("\r");
				} else if (proxys.length == 1) {
					proxys = iplist.split("\n");
				}

				int xcount = 0;
				int bcount = 0;
				for (int i = 0; i < proxys.length; i++) {

					String ips = proxys[i];
//					Ips isip = xhsDao.fetch(Ips.class, Cnd.where("ips", "=", ips));
//					if (isip == null) {
//						isip = new Ips();
//						isip.setIps(ips);
//						isip.setCreate_time(new Date());
//						isip.setOrderby(2);
//						try {
//							xhsDao.fastInsert(isip);
//							xcount++;
//						} catch (Exception e) {
//
//						}
//					}

					Ips bdsip = bdsDao.fetch(Ips.class, Cnd.where("ips", "=", ips));
					if (bdsip == null) {
						bdsip = new Ips();
						bdsip.setIps(ips);
						bdsip.setCreate_time(new Date());
						bdsip.setOrderby(2);
						try {
							bdsDao.fastInsert(bdsip);
							bcount++;
						} catch (Exception e) {

						}
					}

				}
				logger.info("#############IP_HUOJIAN已经注册到缓存:" + xcount + "个IP," + nowtime + "#############");
				logger.info("#############IP_HUOJIAN已经注册到缓存:" + bcount + "个IP," + nowtime + "#############");
			} catch (Exception e) {
				logger.error("", e);
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}
}
